package com.pickgliss.ds 
{
	import com.pickgliss.loaders.ActionInfo;
	import com.pickgliss.loaders.BitmapLoader;
	import com.pickgliss.loaders.PngxLoader;
	import com.pickgliss.utils.StringUtils;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.utils.Dictionary;

	/**
	 * ...
	 * @author Herman
	 */
	public class BitmapLib 
	{
		
		public function BitmapLib() 
		{
			
		}
		
		private var _bitmapLib:Dictionary = new Dictionary(true);
		private var _actionLib:Dictionary = new Dictionary();
		public static const MAX_MER_COUNT:int = 0XFFFFFF;
		
		private static var _instance:BitmapLib;
		public static function get Instance():BitmapLib
		{
			if (_instance == null)
			{
				_instance = new BitmapLib();
			}
			return _instance;
		}
		
		public function hasItem(key:String):Boolean
		{
			key = StringUtils.removeUrlParameter(key);
			return _bitmapLib.hasOwnProperty(key);
		}
		
		public function addItem(key:String, item:BitmapLoader):void
		{
			key = StringUtils.removeUrlParameter(key);
			_bitmapLib[key] = item.content;
			if(item is PngxLoader)
				_actionLib[key] = item["actions"];
		}
		
		public function getBitmapData(key:String):BitmapData
		{
			if(_bitmapLib.hasOwnProperty(key))
			{
				return _bitmapLib[key]["bitmapData"];
			}
			return null;
		}
		
		public function getBitmapInstance(key:String):Bitmap
		{
			if(_bitmapLib.hasOwnProperty(key))
			{
				var bmd:BitmapData = getBitmapData(key);
				return new Bitmap(bmd);
			}
			return null;
		}
		
		public function getBitmap(key:String):Bitmap
		{
			if(_bitmapLib.hasOwnProperty(key))
			{
				return _bitmapLib[key]
			}
			return null;
		}
		
		public function getActions(key:String):Vector.<ActionInfo>
		{
			key = StringUtils.removeUrlParameter(key);
			if(_actionLib.hasOwnProperty(key))
			{
				return _actionLib[key]
			}
			return null;
		}
		
		public function clearnAll():void
		{
			for (var key:String in _bitmapLib)
			{
				delete _bitmapLib[key];
			}
		}
		
	}

}